package com.dcivision.lucene;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.LogFactoryImpl;

public class IndexCommitTask extends Thread {
  
  private Log log = new LogFactoryImpl().getInstance(this.getClass());
  
  private long commitInterval = 30000; //30 S
  
  public IndexCommitTask(long commitInterval) {
    super();
    this.commitInterval = commitInterval;
  }
  
  /**
   * Interval to commit lucene change.
   */
  public synchronized void run() {
    while (true) {
      try {
        IndexFactory.commit();
      } catch (Exception ex) {
        log.error("IndexCommitTask fail to commit.", ex);
      }
      try {
      wait(commitInterval);
      } catch(InterruptedException e) {
        log.error("Index commit task is interrupted.", e);
      }
    }
  }
  
  /**
   * wake up waiting thread to run lucene commit.
   *
   */
  public synchronized void commit() {
    notifyAll();
  }
}
